Strict

Import agfx

Global myTmpScreen:TTmpScreen

Class TAgfx_test_app Extends TFramework

	Method OnCreate:Int()
		
		Super.OnCreate()
				
		'DISPLAY.SetVirtualSize(480,320)
		
		DEBUG = False
		
		myTmpScreen = New TTmpScreen		
		myTmpScreen.Prepare()
		
		Return 0
	End
	
End


Class myPlayerControll Extends TGestureRecorder

	Field obj:TBox2DBody

	Method New(o:TBox2DBody)
		Super.New()
		Self.obj = o
	End
	Method OnRelease:Void()
		Local last:Int = Self.pointArray.Length()
		
		Local gs_x:Int = Self.pointArray[0].position.x
		Local gs_y:Int = Self.pointArray[0].position.y
		Local ge_x:Int = Self.pointArray[last-1].position.x
		Local ge_y:Int = Self.pointArray[last-1].position.y

		Local v_start:TVector2 = New TVector2(gs_x,gs_y)
		Local v_end:TVector2 = New TVector2(ge_x,ge_y)
'		
'		Local v_direction:TVector2 = VecMult(VecSub(v_end,v_start),0.001)
'		
'		Print(v_direction.Length())

		Local v_direction:TVector2 = VecSub(v_end,v_start)
		Local speed:Float = v_direction.Length()*0.001
		If speed>0.15 Then speed = 0.15
		
		v_direction = VecNormalize(v_direction)
		v_direction = VecMult(v_direction,speed)
		
		Self.obj.body.ApplyImpulse(New b2Vec2(v_direction.x, v_direction.y), obj.body.GetWorldCenter())
		
	End Method

End Class

Class TTmpScreen Extends TScreen

	Field tilemap:TTiled_Map
	Field off_x:Float = 0
	Field off_y:Float = 0
	
	Field layer_map_front:Int
	Field layer_map_mid:Int
	Field layer_map_bkg:Int
	Field layer_col_id:Int
	Field col_layer:TTiled_Layer

	Field player:TBox2DBody
	Field player_spr:TSprite
	Field player_control:myPlayerControll


	' START when app is created
	Method Start:Void()

		IMAGES.Load("ball_pink.png","BALL")

		' Create Tiled map
		Self.tilemap = New TTiled_Map()
		Self.tilemap.Load("agfx_platform_test.xml")
		
		Self.layer_map_front = Self.tilemap.GetLayerIDbyName("TileLayer_front")
		Self.layer_map_mid = Self.tilemap.GetLayerIDbyName("TileLayer_middle")
		Self.layer_map_bkg = Self.tilemap.GetLayerIDbyName("TileLayer_bkg")
		Self.layer_col_id = Self.tilemap.GetLayerIDbyName("TileLayer_collision")
		


		'Create Box2D world
		' create world
		BOX2D.CreateWorld(New b2Vec2(0,10),True,100,True)
		
		' convert tile to Static SHape

		Self.col_layer = Self.tilemap.layers.Get(Self.layer_col_id)

		For Local x:Int=0 To Self.col_layer.width-1
			For Local y:Int=0 To Self.col_layer.height-1

				
				Local tile:TTiled_LayerTile = Self.col_layer.GetTile(x,y)
				Local px:Float = tile.tilewidth*x + tile.tilewidth/2.0
				Local py:Float = tile.tileheight*y + tile.tileheight / 2.0
				If tile.id>0 Then
					BOX2D.CreateStaticBox("GROUND_LEFT_"+x+"_"+y,px,py,tile.tilewidth,tile.tileheight)
				EndIF


			Next
		Next

		' create player

		'create dynamic circle from sprite

		Local fixt_ball:b2FixtureDef = New b2FixtureDef()
		fixt_ball.density = 1.0
		fixt_ball.restitution = 0.3
		fixt_ball.friction = 0.2

		Self.player_spr = New TSprite("spr_ball",IMAGES.Find("BALL"),320,240)
		
		'Self.player = BOX2D.CreateDynamicCircle("PHX_BALL",320,240,14,fixt_ball)
		Self.player = BOX2D.CreateDynamicBox("PHX_BALL",320,240,16,16,fixt_ball)

		Self.player_control = New myPlayerControll(Self.player)
		Self.player_control.draw_on=True
		Self.player_control.mode=GM_LINE

	End
	
	' main app logic loop update
	Method Update:Void()
	
		Self.player_control.Update()
		
		Self.player_spr.Update()
		BOX2D.Update()
	
	
		Local tile_offset_x:Int = Self.player.body.GetPosition().x*100
		Local tile_offset_y:Int = Self.player.body.GetPosition().y*100

		Self.tilemap.SetPosition(tile_offset_x-320,tile_offset_y-240)
		
	End
	
	' main app render loop
	Method Render:Void()
		Cls
		Self.player_control.Render()
		Self.tilemap.DrawLayer(Self.layer_map_bkg)
		Self.tilemap.DrawLayer(Self.layer_map_mid)
		Self.tilemap.DrawLayer(Self.layer_map_front)
		'Self.tilemap.DrawLayer(Self.layer_col_id,2,2,True,True)
		'Self.tilemap.DrawObjectGroup(0)
		'Self.tilemap.DrawObjectGroup(1)
		

		BOX2D.Render()
		Self.player_spr.Draw()
		
	End
End
